package com.lock.lockblog.controller.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lock.lockblog.controller.BaseController;
import com.lock.lockblog.model.Admins;
import com.lock.lockblog.service.AdminsService;
import com.lock.lockblog.util.AjaxMessage;
import com.lock.lockblog.util.AppConst;
import com.lock.lockblog.util.SM4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.io.IOException;

/**
 * 后台——管理员类
 * @author Lock-玄清
 * @date 2017年4月14日 上午11:19:04
 */
@Controller
@RequestMapping("/admin")
public class AdminsBackController extends BaseController {
	
	@Autowired
	private AdminsService adminsService;
	
	/**
	 * 跳转到后台管理登录页面   Lock-玄清
	 * @return
	 */
	@RequestMapping(value={"login.html","login"})
	public ModelAndView adminLogin(){
		return new ModelAndView();
	}
	
	/**
	 *验证账户是否存在  Lock-玄清
	 * @return
	 */
	@ResponseBody
	@RequestMapping("gologin.html")
	public AjaxMessage<Object> login(Admins admin, HttpServletRequest request) throws IOException {
		AjaxMessage<Object> ajax = new AjaxMessage<>();
		SM4 sm4 = new SM4();
		String pwd = sm4.encode(admin.getPwd(), AppConst.SM4KEY);
		Admins admins = adminsService.findexist(admin.getName(),pwd);
		if(admins != null){
			ajax.setIs(true);
			request.getSession().setAttribute(AppConst.Session_Admin, admins);
			System.out.println("登录成功！");
		}else{
			ajax.setIs(false);
			ajax.setMsg("* 你输入的用户名或密码有误！");
			System.out.println("登录失败！");
		}
		return ajax;
	}
	
	/**
	 * 跳转到首页   Lock-玄清
	 * @param request
	 * @return
	 */
	@RequestMapping(value={"index.html","index"})
	public ModelAndView gomain(HttpServletRequest request){
		Admins admin = getLoginUser(request);
		if(admin != null){
			ModelAndView result = new ModelAndView("admin/index");
			request.getSession().setAttribute(AppConst.Session_Admin, admin);
			result.addObject("admin", admin);
			return result;
		}else{
			ModelAndView result = new ModelAndView("admin/login");
			result.addObject("message", "* 你输入的用户名或密码有误！");
			return result;
		}
	}
	
	/**
	 * go 修改密码  Lock-玄清
	 * @return
	 */
	@RequestMapping("password.html")
	public ModelAndView pass(){
		return new ModelAndView();
	}
	
	/**
	 * 修改密码   Lock-玄清
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updapass.html")
	public AjaxMessage<Object> updapass(String oldpwd,String newpwd,HttpServletRequest request) throws IOException {
		AjaxMessage<Object> ajax = new AjaxMessage<>();
		SM4 sm4 = new SM4();
		Admins admins = getLoginUser(request);
		if(admins != null){
			oldpwd = sm4.encode(oldpwd, AppConst.SM4KEY);
			if((admins.getPwd()).equals(oldpwd)){
				Admins admin = new Admins();
				admin.setId(admins.getId());
				newpwd = sm4.encode(newpwd, AppConst.SM4KEY);
				admin.setPwd(newpwd);
				int rows = adminsService.updateNotNull(admin);
				if(rows>0){
					ajax.setIs(true);
					ajax.setMsg("修改成功！");
				}else{
					ajax.setIs(false);
					ajax.setMsg("修改失败！");
				}
			}else{
				ajax.setIs(false);
				ajax.setMsg("你输入的原密码错误！");
			}
		}else{
			ajax.setIs(false);
			ajax.setMsg("修改失败！");
		}
		return ajax;
	}
	
	/**
	 * 注销会话  Lock-玄清
	 * @return
	 */
	@RequestMapping("logout.html")
	public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		session.invalidate();
		return new ModelAndView("admin/login");
	}
	
	/**
	 * 跳转到欢迎页面   Lock-玄清
	 * @return
	 */
	@RequestMapping(value={"welcome.html"})
	public ModelAndView welcome(){
		return new ModelAndView();
	}
	
	/**
	 * 跳转到操作成功页面   Lock-玄清
	 * @return
	 */
	@RequestMapping(value={"success.html","success"})
	public ModelAndView success(){
		return new ModelAndView();
	}
	
	/**
	 * 跳转到操作失败页面   Lock-玄清
	 * @return
	 */
	@RequestMapping(value={"error.html","error"})
	public ModelAndView error(){
		return new ModelAndView();
	}
}
